package com.yhb.map.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;

public class ScrollView extends ViewGroup {
	
	private Context context;
	private Scroller mScroller;
	private boolean mFling;
	private GestureDetector gesture;
	private OnScreenChangeListener onScreenChangeListener;
	
	public ScrollView(Context context) {
		super(context);
		this.context = context;
		gesture = new GestureDetector(context, new GestureListener());
		mScroller = new Scroller(context);
	}

	public ScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		gesture = new GestureDetector(context, new GestureListener());
		mScroller = new Scroller(context);
	}

	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		for(int i = 0; i < getChildCount(); i++) {
			View child = getChildAt(i);
			child.measure(right - left, bottom - top);
			child.layout(getWidth()*i, 0, getWidth()*(i+1), getHeight());
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gesture.onTouchEvent(event);
		switch(event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			if(!mFling) {
				scrollToScreen();
			}
			mFling = false;
			break;
		}
		return super.onTouchEvent(event);
	}
	
	private void scrollToScreen() {
		int leftWidth = getScrollX();
		int tabs = leftWidth/getWidth();
		int len = leftWidth - tabs * getWidth();
		if(len < getWidth() / 2) {
			mScroller.startScroll(leftWidth, 0, -len, 0, len*2);
		} else {
			mScroller.startScroll(leftWidth, 0, getWidth()-len, 0, len*2);
			tabs = tabs + 1;
		}
		
		if(onScreenChangeListener != null) {
			onScreenChangeListener.screenChange(tabs, getChildCount());
		}
		invalidate();
	}
	
	@Override
	public void computeScroll() {
		if(mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), 0);
			postInvalidate();
		}
		super.computeScroll();
	}
	
	class GestureListener extends SimpleOnGestureListener {
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if(Math.abs(velocityX) > ViewConfiguration.get(context).getScaledMinimumFlingVelocity()) {
				scrollToScreen();
				mFling = true;
			}
			return super.onFling(e1, e2, velocityX, velocityY);
		}
		
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			if((distanceX > 0 && getScrollX() < (getChildCount()-1) * getWidth())
				|| (distanceX < 0 && getScrollX() > 0)) {
					scrollBy((int)distanceX, 0);
				}
			return super.onScroll(e1, e2, distanceX, distanceY);
		}
	}
	
	public interface OnScreenChangeListener {
		void screenChange(int currentTab, int totalTab);
	}
	
	public void setOnScreenChangeListener(OnScreenChangeListener listener) {
		onScreenChangeListener = listener;
	}
	
	public void initPageControlView() {
		if(onScreenChangeListener!=null){
            onScreenChangeListener.screenChange(1, getChildCount());
        }
	}
	
	
	
	
	
	
	
	
	
}
